package com.bigfreakindeals.Adapters;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.bigfreakindeals.R;
import com.bigfreakindeals.Models.Coupons;
import com.bigfreakindeals.Protocols.SoapHandlers.PostCommentsHandler;

public class CouponsAdapter extends ArrayAdapter<Coupons>{

	
		private LayoutInflater minflater;
		private Coupons[] items;
		Coupons currentcoupon;
		int layoutresources;
		
		static class ViewHolder {
			TextView dealtitle;
			TextView availcoupons;
			TextView expiry;
			ImageView coupon_thumbnail;
		}
		
		public void setItems( Coupons[] items){
			this.items = items;
		}

		public CouponsAdapter(Context context,int layoutresources, Coupons[] items) {
			super(context, layoutresources, items);
			this.items = items;
			this.layoutresources  =layoutresources;
		    minflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		    
	    }
		
	        public Coupons getItem(int position) {
	     	    if (position < this.items.length)
	            return this.items[position];
	        	else return null;
	        	
	        }
		 
	    	@Override
	    public int getCount() {
	    	return items.length;
	    }
	    	
	    public View getView(int position, View convertView, ViewGroup parent) {
	    	ViewHolder holder;
	    	
				convertView = minflater.inflate(layoutresources, null);
				holder = new ViewHolder();
				holder.dealtitle = (TextView) convertView.findViewById(R.id.title);
				holder.availcoupons = (TextView) convertView.findViewById(R.id.availcoupons);
				holder.expiry = (TextView) convertView.findViewById(R.id.expiry);
				holder.coupon_thumbnail = (ImageView) convertView.findViewById(R.id.thumbnail);
				convertView.setTag(holder);

			
			currentcoupon = (Coupons)items[position];
			
			// Bind the data efficiently with the holder.
		
			holder.dealtitle.setText( currentcoupon.dealname);
			holder.availcoupons.setText( currentcoupon.total_count);
			holder.expiry.setText( "Expires"+ currentcoupon.coupons_expirydate);
			
			if (currentcoupon!=null){
				if (currentcoupon.thumbnailmedium!=null){
					holder.coupon_thumbnail.setImageBitmap(currentcoupon.thumbnailmedium);
				}
				
			}
			
			return convertView;
		}

		@Override
		public long getItemId(int arg0) {
			// TODO Auto-generated method stub
			return 0;
		}

		
	}


	

